package org.yx.core;

import io.jboot.Jboot;
import io.jboot.aop.jfinal.JfinalHandlers;
import io.jboot.aop.jfinal.JfinalPlugins;
import io.jboot.app.config.JbootConfigManager;
import io.jboot.core.listener.JbootAppListener;
import io.jboot.web.fixedinterceptor.FixedInterceptors;

import org.yx.core.handler.JsResourceHandler;
import org.yx.utils.Config;

import com.jfinal.config.Constants;
import com.jfinal.config.Interceptors;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSourceFactory;
import com.xiaoleilu.hutool.util.StrUtil;

public class AppListener implements JbootAppListener {
	public void onConstantConfig(Constants constants) {

		constants.setViewType(ViewType.JSP);
		constants.setBaseDownloadPath("files");
	}

	public void onEngineConfig(Engine engine) {
		engine.setDevMode(StrUtil.equals("DEV", Config.getString("jboot.app.mode"), true));
		engine.setBaseTemplatePath(null);
		engine.setSourceFactory(new ClassPathSourceFactory());
	}

	public void onFixedInterceptorConfig(FixedInterceptors fixedInterceptors) {
		// TODO Auto-generated method stub

	}

	public void onHandlerConfig(JfinalHandlers handlers) {
		handlers.add(new JsResourceHandler());
		String cp = Jboot.configValue("jboot.server.contextPath");
		handlers.add(new ContextPathHandler(Jboot.configValue("jboot.server.contextPath")));
	}

	public void onInit() {
		// TODO Auto-generated method stub
	}

	public void onInterceptorConfig(Interceptors interceptors) {
		// TODO Auto-generated method stub

	}

	public void onPluginConfig(JfinalPlugins plugins) {
		// TODO Auto-generated method stub

	}

	public void onRouteConfig(Routes routes) {
		// TODO Auto-generated method stub

	}

	public void onStart() {

		if (!StrUtil.isBlank(Jboot.configValue("broker.address"))) {
			try {
				BusServer.getInstance().start(JbootConfigManager.me().getProperties());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void onStartBefore() {
		// TODO Auto-generated method stub

	}

	public void onStop() {
		if (!StrUtil.isBlank(Jboot.configValue("broker.address"))) {
			BusServer.getInstance().stop();
		}

	}
}
